#include <cstdio>
#include <algorithm>

using namespace std;

struct Node
{
    char name[ 81 ];
    int count;
};

struct ChanDi
{
    Node fruit[ 101 ];
    int pz;
    char name[ 81 ];
};

bool operator < ( Node a, Node b )
{
    return strcmp( a.name, b.name ) < 0;
}

bool operator < ( ChanDi a, ChanDi b )
{
    return strcmp( a.name, b.name ) < 0;
}

int main( )
{
    int n, m, s, i, j, c;
    char fruit[ 81 ], place[ 81 ];
    ChanDi p[ 100 ];
    scanf("%d", &n);
    while ( n-- )
    {
        scanf("%d", &m);
        c = 0;
        while ( m-- )
        {
            scanf("%s%s%d", fruit, place, &s );
            for ( i = 0; i < c; i++ )
                if ( strcmp( place, p[ i ].name ) == 0 )
                {
                    for ( j = 0; j < p[ i ].pz; j++ )
                        if ( strcmp( fruit, p[ i ].fruit[ j ].name ) == 0 )
                        {
                            p[ i ].fruit[ j ].count += s;
                            break;
                        }
                    if ( j == p[ i ].pz )
                    {
                        strcpy( p[ i ].fruit[ j ].name, fruit );
                        p[ i ].fruit[ j ].count = s;
                        p[ i ].pz++;
                    }
                    break;
                }
            if ( i == c )
            {
                strcpy( p[ i ].name, place );
                c++;
                p[ i ].pz = 1;
                strcpy( p[ i ].fruit[ 0 ].name, fruit );
                p[ i ].fruit[ 0 ].count = s;
            }
        }
        sort( p, p + c );
        for ( i = 0; i < c; i++ )
            sort( p[ i ].fruit, p[ i ].fruit + p[ i ].pz );
        for ( i = 0; i < c; i++ )
        {
            printf("%s\n", p[ i ].name);
            for ( j = 0; j < p[ i ].pz; j++ )
                printf("   |----%s(%d)\n", p[ i ].fruit[ j ].name, p[ i ].fruit[ j ].count);
        }
        if ( n )
            printf("\n");
    }
    return 0;
}
